﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System.Text.RegularExpressions;
using System.Windows.Media.Imaging;
using ODS.WebService.AccountFuntions;
using ODS.Utils;
using ODS.Config;

namespace ODS
{
    public partial class RegisterPage : PhoneApplicationPage
    {
        bool IsLoginNameOk = false;
        bool IsNewPasswordOk = false;
        bool IsReNewPasswordOk = false;

        public RegisterPage()
        {
            InitializeComponent();
        }

        private void btn_back_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            // TODO: Add event handler implementation here.
            NavigationService.GoBack();
        }

        #region New password
        private void NewPasswordLostFocus(object sender, RoutedEventArgs e)
        {
            CheckNewPasswordWatermark();
        }

        public void CheckNewPasswordWatermark()
        {
            var passwordEmpty = string.IsNullOrEmpty(NewPassword.Password);
            NewPasswordWatermark.Opacity = passwordEmpty ? 100 : 0;
            NewPassword.Opacity = passwordEmpty ? 0 : 100;
        }

        private void NewPasswordGotFocus(object sender, RoutedEventArgs e)
        {
            NewPasswordWatermark.Opacity = 0;
            NewPassword.Opacity = 100;
        }

        #endregion

        #region Renew password
        private void RenewPasswordLostFocus(object sender, RoutedEventArgs e)
        {
            CheckRenewPasswordWatermark();
        }

        public void CheckRenewPasswordWatermark()
        {
            var passwordEmpty = string.IsNullOrEmpty(RenewPassword.Password);
            RenewPasswordWatermark.Opacity = passwordEmpty ? 100 : 0;
            RenewPassword.Opacity = passwordEmpty ? 0 : 100;
        }

        private void RenewPasswordGotFocus(object sender, RoutedEventArgs e)
        {
            RenewPasswordWatermark.Opacity = 0;
            RenewPassword.Opacity = 100;
        }

        #endregion

        private async void btn_apply_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            if (!validateInput())
                return;


            showWaiting();

            String countryCode= "702";
            InfoResult result = await RegisterUserService.RegisterUser(countryCode, tb_loginName.MyText, NewPassword.Password);
            if (result.IsSuccess)
            {
                if ((String)result.ReturnedObject == Constants.SUCCESS)
                {
                    MessageBox.Show("Register User successful.");

                    Setting.Set(Constants.KEY_USERNAME, App.userLogin.UserLoginName);
                    Setting.Set(Constants.KEY_PASSWORD, App.userLogin.UserPassword);

                    this.NavigationService.GoBack();
                }
                else
                {
                    MessageBox.Show((String)result.ReturnedObject);
                }
            }
            else
            {
                MessageBox.Show(result.Error);
            }

            hiddenWaiting();
        }

        private void tb_loginName_TextChanged(object sender, System.Windows.Controls.TextChangedEventArgs e)
        {
            if (IsLoginNameOk = Regex.IsMatch(tb_loginName.MyText, @"^((([\w]+\.[\w]+)+)|([\w]+))@(([\w]+\.)+)([A-Za-z]{1,3})$"))
            {
                img_checkLoginName.Source = new BitmapImage(new Uri("/Assets/images/tick icon.png", UriKind.Relative));
            }
            else
            {
                img_checkLoginName.Source = new BitmapImage(new Uri("/Assets/images/grey tick.png", UriKind.Relative));
            }
        }

        private void NewPassword_PasswordChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            if (IsNewPasswordOk =  Regex.IsMatch(NewPassword.Password, @"^(.{8,})$"))
            {
                img_newPassword.Source = new BitmapImage(new Uri("/Assets/images/tick icon.png", UriKind.Relative));
            }
            else
            {
                img_newPassword.Source = new BitmapImage(new Uri("/Assets/images/grey tick.png", UriKind.Relative));
            }

            if (IsReNewPasswordOk = (Regex.IsMatch(RenewPassword.Password, @"^(.{8,})$") && RenewPassword.Password.Equals(NewPassword.Password)))
            {
                img_reNewPassword.Source = new BitmapImage(new Uri("/Assets/images/tick icon.png", UriKind.Relative));
            }
            else
            {
                img_reNewPassword.Source = new BitmapImage(new Uri("/Assets/images/grey tick.png", UriKind.Relative));
            }
        }

        private void RenewPassword_PasswordChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            if (IsReNewPasswordOk = (Regex.IsMatch(RenewPassword.Password, @"^(.{8,})$") && RenewPassword.Password.Equals(NewPassword.Password)))
            {
                img_reNewPassword.Source = new BitmapImage(new Uri("/Assets/images/tick icon.png", UriKind.Relative));
            }
            else
            {
                img_reNewPassword.Source = new BitmapImage(new Uri("/Assets/images/grey tick.png", UriKind.Relative));
            }
        }

        bool validateInput()
        {
            if (!IsLoginNameOk)
            {
                MessageBox.Show("Username alpha numuric only, please try again.");
                return false;
            }


            if (!IsNewPasswordOk)
            {
                MessageBox.Show("Incorrect New Password, please try again.");
                return false;
            }

            if (!IsReNewPasswordOk)
            {
                MessageBox.Show("Incorrect Retype Password, please try again.");
                return false;
            }

            return true;
        }

        void showWaiting()
        {
            progressBar_waiting.Visibility = Visibility.Visible;
            tb_loginName.IsEnabled = false;
            RenewPassword.IsEnabled = false;
            NewPassword.IsEnabled = false;
        }

        void hiddenWaiting()
        {
            progressBar_waiting.Visibility = Visibility.Collapsed;
            tb_loginName.IsEnabled = true;
            RenewPassword.IsEnabled = true;
            NewPassword.IsEnabled = true;
        }

    }
}